package com.sun.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.constant.MessageConstant;
import com.sun.constant.StatusConstant;
import com.sun.dto.ArtworkDTO;
import com.sun.dto.ArtworkPageQueryDTO;
import com.sun.entity.Artwork;
import com.sun.entity.ArtworkDes;
import com.sun.entity.Setmeal;
import com.sun.entity.SetmealArtwork;
import com.sun.exception.DeletionNotAllowedException;
import com.sun.mapper.ArtworkDesMapper;
import com.sun.mapper.ArtworkMapper;
import com.sun.mapper.SetmealArtworkMapper;
import com.sun.mapper.SetmealMapper;
import com.sun.result.PageResult;
import com.sun.service.ArtworkService;
import com.sun.vo.ArtworkVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ArtworkServiceImpl implements ArtworkService {

    @Autowired
    private ArtworkMapper artworkMapper;

    @Autowired
    private ArtworkDesMapper artworkDesMapper;

    @Autowired
    private SetmealArtworkMapper setmealArtworkMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增艺术品和对应的详细信息
     * @param artworkDTO
     */
    @Override
    public void saveWithDes(ArtworkDTO artworkDTO) {
        Artwork artwork = new Artwork();
        BeanUtils.copyProperties(artworkDTO, artwork);

        // 向艺术品表中插入数据
        artworkMapper.insert(artwork);

        // 获取insert语句生成的主键值
        Long artworkId = artwork.getId();


        List<ArtworkDes> des = artworkDTO.getFlavors();
        if (des != null && des.size() > 0) {
            des.forEach(artworkDes -> artworkDes.setArtworkId(artworkId));
        }
        log.info("插入数据的集合:{}",des);
        //向详细信息表中插入数据
        artworkDesMapper.insertBatch(des);

    }

    /**
     * 艺术品分页查询
     * @param artworkPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(ArtworkPageQueryDTO artworkPageQueryDTO) {
        PageHelper.startPage(artworkPageQueryDTO.getPage(),artworkPageQueryDTO.getPageSize());
        Page<ArtworkVO> page =  artworkMapper.pageQuery(artworkPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 艺术品批量删除
     * @param ids
     */
    @Transactional //事务
    public void deleteBatch(List<Long> ids) {
        //判断当前艺术品是否能够删除 -- 是否是起售状态
        for (Long id : ids) {
            Artwork artwork = artworkMapper.getById(id);
            if (artwork.getStatus() == StatusConstant.ENABLE){
                // 当前艺术品处于起售状态, 不能删除
                throw new DeletionNotAllowedException(MessageConstant.ARTWORK_ON_SALE);
            }
        }

        //判断当前艺术品是否能够删除 -- 是否有关联套餐
        List<Long> setmealIds = setmealArtworkMapper.getSidsByAids(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            //表示当前的艺术品被套餐关联了,也就是不能删除
            throw new DeletionNotAllowedException(MessageConstant.ARTWORK_BE_RELATED_BY_SETMEAL);
        }

        //删除艺术品表中的数据 -- 删两张表中的数据 artwork / artwork_des
        for(Long id : ids){
            //删除艺术品
            artworkMapper.deleteById(id);
            //删除艺术品下对应的详细的信息
            artworkDesMapper.deleteByArtworkId(id);
        }
    }

    /**
     * 根据id查询艺术品和对应的详细信息
     * @param id
     * @return
     */
    @Override
    public ArtworkVO getByIdWithDes(Long id) {
        // 根据id查询艺术品数据
        Artwork artwork = artworkMapper.getById(id);

        //根据艺术品id查询详细信息des
        List<ArtworkDes> artworkDes = artworkDesMapper.getByArtworkId(id);

        //将查询到的数据封装到VO对象中
        ArtworkVO artworkVO = new ArtworkVO();
        BeanUtils.copyProperties(artwork,artworkVO);
        artworkVO.setFlavors(artworkDes);
        return artworkVO;
    }

    /**
     * 修改艺术品
     * @param artworkDTO
     */
    @Override
    public void updateWithDes(ArtworkDTO artworkDTO) {
        Artwork artwork = new Artwork();
        BeanUtils.copyProperties(artworkDTO, artwork);

        //修改艺术品基础部分的信息
        artworkMapper.update(artwork);

        //删除原有的数据 -> 详细信息
        artworkDesMapper.deleteByArtworkId(artworkDTO.getId());

        //重新插入新的详细信息描述
        List<ArtworkDes> dess = artworkDTO.getFlavors();
        if (dess != null && dess.size() > 0) {
            dess.forEach(artworkDes -> {
                artworkDes.setArtworkId(artworkDTO.getId());
            });
        }

        //向详细信息表插入数据
        artworkDesMapper.insertBatch(dess);
    }

    /**
     * 艺术品起售停售
     *
     * @param status
     * @param id
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Artwork artwork = Artwork.builder()
                .id(id)
                .status(status)
                .build();
        artworkMapper.update(artwork);

        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前艺术品的套餐也停售
            List<Long> ArtworkIds = new ArrayList<>();
            ArtworkIds.add(id);
            // select setmeal_id from setmeal_Artwork where Artwork_id in (?,?,?)
            List<Long> setmealIds = setmealArtworkMapper.getSidsByAids(ArtworkIds);
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }


    /**
     * 根据分类id查询艺术品
     * @param categoryId
     * @return
     */
    public List<Artwork> list(Long categoryId) {
        Artwork artwork = Artwork.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return artworkMapper.list(artwork);
    }

    /**
     * 条件查询艺术品和详细信息
     * @param artwork
     * @return
     */
    public List<ArtworkVO> listWithFlavor(Artwork artwork) {
        List<Artwork> artworkList = artworkMapper.list(artwork);

        List<ArtworkVO> artworkVOList = new ArrayList<>();

        for (Artwork d : artworkList) {
            ArtworkVO artworkVO = new ArtworkVO();
            BeanUtils.copyProperties(d,artworkVO);

            //根据艺术品id查询对应的详细信息
            List<ArtworkDes> flavors = artworkDesMapper.getByArtworkId(d.getId());

            artworkVO.setFlavors(flavors);
            artworkVOList.add(artworkVO);
        }

        return artworkVOList;
    }


}
